﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;
using System.Text.RegularExpressions;
using MyWeb.Data;
using MyWeb.Business;

namespace Web.Admins
{
    public partial class Quangcao : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["folder"] = "Menu"; 
                
                
                BindGrid();
            }
        }
        
        private void BindGrid()
        {
            grdMenu0.DataSource = ProductService.Product_GetByTop("", "", "[Date] Desc");
           
             grdMenu0.DataBind();
            grdMenu.DataSource = CustomersService.Customers_GetByAll();
            grdMenu.DataBind();
           
        }

        protected void mail_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridItem item = default(DataGridItem);
                DataGridItem item0 = default(DataGridItem);
                for (int i = 0; i < grdMenu.Items.Count; i++)
                {
                    item = grdMenu.Items[i];
                    if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                    {
                        if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                        {
                            for (int j = 0; j < grdMenu0.Items.Count; i++)
                            {
                                item0 = grdMenu0.Items[i];
                                if (item0.ItemType == ListItemType.AlternatingItem | item0.ItemType == ListItemType.Item)
                                    {
                                         if (((CheckBox)item0.FindControl("ChkSelect")).Checked)
                                            {
                                             
                                                string strId = item.Cells[1].Text;
                                                string strId0 = item0.Cells[1].Text;
                                                DataTable dt = CustomersService.Customers_GetById(strId);
                                                DataTable dt0 = ProductService.Product_GetById(strId0);
                                                string ycus = dt.Rows[0]["year"].ToString();
                                                string ypro = dt0.Rows[0]["Age"].ToString();
                                                int y2 = Convert.ToInt16(ypro);
                                                int y = DateTime.Now.Year - Convert.ToInt16(ycus);
                                                int y3 = y - y2;
                                               
                                                if (y>=y2)
                                                {
                                                    try
                                                    {


                                                        string to = dt.Rows[0]["Email"].ToString();
                                                        string from = "odin.utmh@gmail.com";
                                                        MailMessage mailMsg = new MailMessage();
                                                        mailMsg.From = new MailAddress(from, "Thế Giới GamePC");
                                                        mailMsg.To.Add(to);
                                                        mailMsg.Subject = "Thế Giới GamePC: Game Mới:"+dt0.Rows[0]["Name"].ToString();
                                                        mailMsg.IsBodyHtml = true;
                                                        mailMsg.Body = TextBox1.Text + dt0.Rows[0]["Name"].ToString() + "<br><br><img src=http://localhost:7623" + dt0.Rows[0]["Image"].ToString() + "/>" + "<br> http://localhost:7623/ChiTietSanPham.aspx?pid=" + dt0.Rows[0]["Id"].ToString();
                                                        mailMsg.Priority = MailPriority.Normal;
                                                        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                                                        client.Credentials = new System.Net.NetworkCredential("odin.utmh@gmail.com", "ungthimyhuong");
                                                        client.DeliveryMethod = SmtpDeliveryMethod.Network;

                                                        client.EnableSsl = true;
                                                        client.Send(mailMsg);
                                                    }
                                                    catch { }
                                                }
                                         }
                                }

                                
                            }
                            
                        }
                    }
                }
               
                
            }
            catch { }
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridItem item = default(DataGridItem);
                DataGridItem item0 = default(DataGridItem);
                for (int i = 0; i < grdMenu.Items.Count; i++)
                {
                    item = grdMenu.Items[i];
                    if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                    {
                        if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                        {
                            for (int j = 0; j < grdMenu0.Items.Count; i++)
                            {
                                item0 = grdMenu0.Items[i];
                                if (item0.ItemType == ListItemType.AlternatingItem | item0.ItemType == ListItemType.Item)
                                {
                                    if (((CheckBox)item0.FindControl("ChkSelect")).Checked)
                                    {

                                        string strId = item.Cells[1].Text;
                                        string strId0 = item0.Cells[1].Text;
                                        DataTable dt = CustomersService.Customers_GetById(strId);
                                        DataTable dt0 = ProductService.Product_GetById(strId0);
                                        
                                            try
                                            {


                                                string to = dt.Rows[0]["Email"].ToString();
                                                string from = "odin.utmh@gmail.com";
                                                MailMessage mailMsg = new MailMessage();
                                                mailMsg.From = new MailAddress(from, "Thế Giới GamePC");
                                                mailMsg.To.Add(to);
                                                mailMsg.Subject = "Thế Giới GamePC: Game Mới:" + dt0.Rows[0]["Name"].ToString();
                                                mailMsg.IsBodyHtml = true;
                                                mailMsg.Body = TextBox1.Text + dt0.Rows[0]["Name"].ToString() + "<br><br><img src=http://localhost:7623" + dt0.Rows[0]["Image"].ToString() + "/>" + "<br> http://localhost:7623/ChiTietSanPham.aspx?pid=" + dt0.Rows[0]["Id"].ToString();
                                                mailMsg.Priority = MailPriority.Normal;
                                                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                                                client.Credentials = new System.Net.NetworkCredential("odin.utmh@gmail.com", "ungthimyhuong");
                                                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                                                client.EnableSsl = true;
                                                client.Send(mailMsg);
                                            }
                                            catch { }
                                        
                                    }
                                }


                            }

                        }
                    }
                }


            }
            catch { }
            
        }

        
        
        
    }
}